package com.dtzhejiang.social.supervision.risk.engine.core.service.impl;

import com.dtzhejiang.social.supervision.risk.engine.common.domain.po.SpiderHospitalBusinessLicenseEntity;
import com.dtzhejiang.social.supervision.risk.engine.common.domain.po.SpiderHospitalPracticeLicenseEntity;
import com.dtzhejiang.social.supervision.risk.engine.core.mapper.SpiderHospitalBusinessLicenseMapper;
import com.dtzhejiang.social.supervision.risk.engine.core.mapper.SpiderHospitalPracticeLicenseMapper;
import com.dtzhejiang.social.supervision.risk.engine.core.service.LicenseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: chenwenhao
 * Date: 10:18 2020/11/3
 * Content: 店铺营业执照和执业许可服务实现类
 */
@Service
public class LicenseServiceImpl implements LicenseService {

    @Resource
    private SpiderHospitalBusinessLicenseMapper businessLicenseMapper;
    @Resource
    private SpiderHospitalPracticeLicenseMapper practiceLicenseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateBusinessLicense(SpiderHospitalBusinessLicenseEntity businessLicense) {
        SpiderHospitalBusinessLicenseEntity businessLicenseInDb = businessLicenseMapper.selectByShopId(businessLicense.getShopId());
        if (businessLicenseInDb == null) {
            businessLicenseMapper.insertSelective(businessLicense);
        } else {
            businessLicense.setId(businessLicenseInDb.getId());
            businessLicenseMapper.updateByPrimaryKeySelective(businessLicense);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdatePracticeLicense(SpiderHospitalPracticeLicenseEntity practiceLicense) {
        SpiderHospitalPracticeLicenseEntity practiceLicenseInDb = practiceLicenseMapper.selectByShopId(practiceLicense.getShopId());
        if (practiceLicenseInDb == null) {
            practiceLicenseMapper.insertSelective(practiceLicense);
        } else {
            practiceLicense.setId(practiceLicenseInDb.getId());
            practiceLicenseMapper.updateByPrimaryKeySelective(practiceLicense);
        }
    }

    @Override
    public List<String> getAllShopIdsForBusinessLicense() {
        return businessLicenseMapper.selectAllShopIds();
    }

    @Override
    public List<String> getAllShopIdsForPracticeLicense() {
        return practiceLicenseMapper.selectAllShopIds();
    }

    @Override
    public List<SpiderHospitalBusinessLicenseEntity> getAllBusinessLicense() {
        return businessLicenseMapper.selectAllBusinessLicense();
    }

    @Override
    public List<SpiderHospitalPracticeLicenseEntity> getAllPracticeLicense() {
        return practiceLicenseMapper.selectAllPracticeLicense();
    }
}
